package com.github.nacosone;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.github.nacosone.core.AbstractCoreController;

import java.util.Collections;
import java.util.Scanner;

//演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

 /**
  * <p>
  * 读取控制台内容
  * </p>
  */
 public static String scanner(String tip) {
     Scanner scanner = new Scanner(System.in);
     StringBuilder help = new StringBuilder();
     help.append("请输入" + tip + "：");
     System.out.println(help.toString());
     if (scanner.hasNext()) {
         String ipt = scanner.next();
         if (StringUtils.isNotBlank(ipt)) {
             return ipt;
         }
     }
     throw new MybatisPlusException("请输入正确的" + tip + "！");
 }

 public static void main(String[] args) {

     // 数据源配置.setDriverName("com.mysql.cj.jdbc.Driver")
     DataSourceConfig dsc = new DataSourceConfig
             .Builder("jdbc:mysql://localhost:3326/publiccms?useUnicode=true&useSSL=false&characterEncoding=utf8","root","root")
             .build();

     // 代码生成器
     AutoGenerator mpg = new AutoGenerator(dsc);

     // 全局配置
     String projectPath = System.getProperty("user.dir");
     GlobalConfig gc = new GlobalConfig
             .Builder()
             .outputDir(projectPath + "/src/main/java")
             .author("brucezhang")
             .enableSwagger()
             .build();
    mpg.global(gc);
     // 包配置
     PackageConfig pc = new PackageConfig
             .Builder()
             .parent("com.github.nacosone")
             .entity("entity")
             .mapper("mapper")
             .service("service")
             .controller("ui")
             .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/main/resources/com/github/nacosone/mapper"))
             .build();
    mpg.packageInfo(pc);


     // 配置模板
     TemplateConfig templateConfig = new TemplateConfig
             .Builder()
             .build();
     mpg.template(templateConfig);

     // 策略配置
     StrategyConfig strategy = new StrategyConfig
             .Builder()
             .entityBuilder()
             .naming(NamingStrategy.underline_to_camel)
             .enableLombok()
             .columnNaming(NamingStrategy.underline_to_camel)
             .mapperBuilder()
             .enableMapperAnnotation()
             .enableBaseColumnList()
             .enableBaseResultMap()
             .controllerBuilder()
             .enableRestStyle()
             .superClass(AbstractCoreController.class)
             .build();
     mpg.strategy(strategy);
     mpg.execute(new VelocityTemplateEngine());
 }

}